package com.saver.modules.sys.controller;

import com.saver.common.annotation.SysOperationLog;
import com.saver.common.utils.PageUtils;
import com.saver.common.utils.R;
import com.saver.modules.sys.dao.SysRoleDao;
import com.saver.modules.sys.dao.SysUserDao;
import com.saver.modules.sys.entity.SysUserEntity;
import com.saver.modules.sys.form.PasswordForm;
import com.saver.modules.sys.service.SysRoleService;
import com.saver.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * yuxiaolong
 *
 * @author yuxiaolong
 * @email 1005471232@qq.com
 * @date 2018年08月25日 下午7:08
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserDao sysUserDao;

    /**
     * 登录时的获取用户信息
     */
    @GetMapping("/getInfo")
    public R getInfo() {
        return R.ok().put("user", getUser());
    }


    /**
     * 上传头像
     *
     * @param multipartFile
     * @return
     */
    @PostMapping("/uploadAvatar")
    public R uploadAvatar(@RequestParam("file") MultipartFile multipartFile) {
        return sysUserService.uploadAvatar(multipartFile, getUserId());
    }

    /**
     * 修改用户基本信息
     */
    @PostMapping("/updateUser")
    public R updateUser(@RequestBody SysUserEntity user) {
        Long userId = getUserId();
        SysUserEntity byUserId = sysUserDao.findByUserId(userId);
        byUserId.setName(user.getName());
        byUserId.setMobile(user.getMobile());
        byUserId.setEmail(user.getEmail());
        sysUserDao.save(byUserId);
        return R.ok();
    }

    /**
     * 修改用户密码
     */
    @SysOperationLog("修改用户密码")
    @PostMapping("/updatePassword")
    public R updatePassword(@RequestBody PasswordForm form) {
        return sysUserService.updatePassword(form,getUserId());
    }

    /**
     * 分页
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //只有超级管理员，才能查看所有管理员列表
        PageUtils page = sysUserService.queryPage(params,getUserId());
        return R.ok().put("page", page);
    }


    /**
     * 查询用户信息
     */
    @GetMapping("/info/{userId}")
    public R info(@PathVariable("userId") Long userId) {
       return sysUserService.info(userId,getUser());
    }

    /**
     * 保存用户
     */
    @SysOperationLog("保存用户")
    @PostMapping("/save")
    public R save(@RequestBody SysUserEntity user) {
        return sysUserService.save(user,getUserId());
    }

    /**
     * 修改用户
     */
    @SysOperationLog("修改用户")
    @PostMapping("/update")
    public R update(@RequestBody SysUserEntity user) {
        sysUserService.update(user);
        return R.ok();
    }

    /**
     * 修改用户状态
     */
    @SysOperationLog("修改用户状态")
    @PostMapping("/updateStatus")
    public R updateStatus(@RequestBody SysUserEntity sysUserEntity) {
        return sysUserService.updateStatus(sysUserEntity);
    }

    /**
     * 删除用户
     */
    @SysOperationLog("删除用户")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] userIds) {
        return sysUserService.deleteBatch(userIds,getUserId());
    }

}
